package cn.com.franke.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.serializer.SerializerFeature;

import java.io.*;
import java.util.List;
import java.util.Map;

/**
 * fastJson工具类
 */
public final class FastJsonUtil {

    public static <T> void writeJsonToFile(T t, File file) throws IOException {
        String jsonStr = JSONObject.toJSONString(t, SerializerFeature.PrettyFormat);
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
        bw.write(jsonStr);
        bw.close();
    }

    public static <T> void writeJsonToFile(T t, String filename) throws IOException {
        writeJsonToFile(t, new File(filename));
    }

    public static <T> T readJsonFromFile(Class<T> cls, File file) throws IOException {
        StringBuilder strBuilder = new StringBuilder();
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
        String line = null;
        while ((line = br.readLine()) != null) {
            strBuilder.append(line);
        }
        br.close();
        return JSONObject.parseObject(strBuilder.toString(), cls);
    }

    public static <T> T readJsonFromFile(Class<T> cls, String filename) throws IOException {
        return readJsonFromFile(cls, new File(filename));
    }

    public static <T> T readJsonFromFile(TypeReference<T> typeReference, File file) throws IOException {
        StringBuilder strBuilder = new StringBuilder();
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
        String line = null;
        while ((line = br.readLine()) != null) {
            strBuilder.append(line);
        }
        br.close();
        return JSONObject.parseObject(strBuilder.toString(), typeReference);
    }

    public static <T> T readJsonFromFile(TypeReference<T> typeReference, String filename) throws IOException {
        return readJsonFromFile(typeReference, new File(filename));
    }

    /**
     * 功能描述：把JSON数据转换成普通字符串列表
     *
     * @param jsonData JSON数据
     * @return
     * @throws Exception
     * @author myclover
     */
    public static List<String> getStringList(String jsonData) throws Exception {
        return JSON.parseArray(jsonData, String.class);
    }

    /**
     * 功能描述：把JSON数据转换成指定的java对象
     *
     * @param jsonData JSON数据
     * @param clazz    指定的java对象
     * @return
     * @throws Exception
     * @author myclover
     */
    public static <T> T getSingleBean(String jsonData, Class<T> clazz)
            throws Exception {
        return JSON.parseObject(jsonData, clazz);
    }

    /**
     * 功能描述：把JSON数据转换成指定的java对象列表
     *
     * @param jsonData JSON数据
     * @param clazz    指定的java对象
     * @return
     * @throws Exception
     * @author myclover
     */
    public static <T> List<T> getBeanList(String jsonData, Class<T> clazz)
            throws Exception {
        return JSON.parseArray(jsonData, clazz);
    }

    /**
     * 功能描述：把JSON数据转换成较为复杂的java对象列表
     *
     * @param jsonData JSON数据
     * @return
     * @throws Exception
     * @author myclover
     */
    public static List<Map<String, Object>> getBeanMapList(String jsonData)
            throws Exception {
        return JSON.parseObject(jsonData,
                new TypeReference<List<Map<String, Object>>>() {
                });
    }

    /**
     * 将网络请求下来的数据用fastjson处理空的情况，并将时间戳转化为标准时间格式
     *
     * @param result
     * @return
     */
    public static String dealResponseResult(String result) {
        result = JSONObject.toJSONString(result,
                SerializerFeature.WriteClassName,
                SerializerFeature.WriteMapNullValue,
                SerializerFeature.WriteNullBooleanAsFalse,
                SerializerFeature.WriteNullListAsEmpty,
                SerializerFeature.WriteNullNumberAsZero,
                SerializerFeature.WriteNullStringAsEmpty,
                SerializerFeature.WriteDateUseDateFormat,
                SerializerFeature.WriteEnumUsingToString,
                SerializerFeature.WriteSlashAsSpecial,
                SerializerFeature.WriteTabAsSpecial);
        return result;
    }

    /**
     * 将一个 Object 或者List对象转化为JSONObject或者JSONArray
     *
     * @param ObjOrList Object 或者List对象
     * @return
     */
    public static Object toJSON(Object ObjOrList) {
        Object obj = null;

        try {
            obj = JSON.toJSON(ObjOrList);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return obj;
    }


    /**
     * 将一个 Object 或者List对象转化为JSOObject或者JSONArray
     *
     * @param ObjOrList Object 或者List对象 或者hashmap 但是如果是set  就会有问题
     * @return
     */
    public static String toJSONStr(Object ObjOrList) {
        String jsonstr = "";

        try {
            jsonstr = JSON.toJSONString(ObjOrList);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return jsonstr;
    }


    //第二部分字符串转  obj list


    /**
     * 字符串转obj
     *
     * @param jsonstr
     * @param clazz
     * @return
     */
    public static Object parseToObject(String jsonstr, Class<?> clazz) {
        Object parseObj = null;
        try {
            parseObj = JSON.parseObject(jsonstr, clazz);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return parseObj;
    }

    /**
     * 字符串转list
     *
     * @param jsonstr
     * @param clazz
     * @return
     */
    public static List<?> parseToList(String jsonstr, Class<?> clazz) {
        List<?> parseObj = null;
        try {
            parseObj = JSON.parseArray(jsonstr, clazz);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return parseObj;
    }


    //第三部分  字符串转JSONObj  或者JSONArray

    /**
     * 字符串转jsonobj
     *
     * @param jsonstr
     * @return
     */
    public static JSONObject parseToJSONObejct(String jsonstr) {
        JSONObject parseObj = null;
        try {
            parseObj = JSON.parseObject(jsonstr);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return parseObj;
    }

    /**
     * 字符串转list
     *
     * @param jsonstr
     * @return
     */
    public static JSONArray parseToJSONArray(String jsonstr) {
        JSONArray parseObj = null;
        try {
            parseObj = JSON.parseArray(jsonstr);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return parseObj;
    }


    //第四部分 com.alibaba包下 JSONObj 或者JSONArr 转 javabean或者 java array

    /**
     * @param jsonObj
     * @param obj
     * @return
     */
    public static Object parseToObject(JSONObject jsonObj, Class<?> obj) {
        Object parseObj = null;
        try {
            parseObj = JSON.parseObject(jsonObj + "", obj);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return parseObj;
    }

    /**
     * @param jsonArr
     * @param obj
     * @return
     */
    public static List<?> parseToList(JSONArray jsonArr, Class<?> obj) {
        List list = null;

        try {
            list = JSON.parseArray(jsonArr + "", obj);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return list;
    }


}
